package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class KEXINHUYUAttr implements AdapterAttr {


    public static void main(String[] args) {

    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, String> params = new HashMap<>();
        Map<String, Object> header = new HashMap<>();
        String appkey = salePartner.getSaleKey();
        String APPSECRET = salePartner.getOther();
        long l = System.currentTimeMillis();
        String timestamp = String.valueOf(l);

        String cover = APPSECRET+"kxhy-app-key="+appkey+"&kxhy-nonce=02aca0b7&kxhy-timestamp="+timestamp+APPSECRET;
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        header.put("kxhy-app-key",appkey);
        header.put("kxhy-nonce","02aca0b7");
        header.put("kxhy-timestamp",timestamp);
        header.put("kxhy-sign",sign);

        String result = HttpRequest.post("https://kxxm.kxhyapp.com/openApi/games?page=1&limit=5000").header("kxhy-app-key", appkey).header("kxhy-nonce", "02aca0b7")
                .header("kxhy-timestamp", timestamp).header("kxhy-sign", sign).execute().body();
        String data = JSONObject.parseObject(result).getString("data");
        List<JSONObject> jsonObjects = JSONObject.parseArray(JSONObject.parseObject(data).getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();

        params.put("game_id",gameId);
        String cover1 = MapUntils.getCover(params);
        System.out.println(cover1);

        Map<String, Object> header = new HashMap<>();
        String appkey = salePartner.getSaleKey();
        String APPSECRET = salePartner.getOther();
        long l = System.currentTimeMillis();
        String timestamp = String.valueOf(l);

        String cover = APPSECRET+cover1+"kxhy-app-key="+appkey+"&kxhy-nonce=02aca0b7&kxhy-timestamp="+timestamp+APPSECRET;
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        header.put("kxhy-app-key",appkey);
        header.put("kxhy-nonce","02aca0b7");
        header.put("kxhy-timestamp",timestamp);
        header.put("kxhy-sign",sign);

        String response = HttpRequest.post("https://kxxm.kxhyapp.com/openApi/gameArea").header("kxhy-app-key", appkey).header("kxhy-nonce", "02aca0b7")
                .header("kxhy-timestamp", timestamp).header("kxhy-sign", sign).body(JSONObject.toJSONString(params)).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        String string = JSONObject.parseObject(jsonObject.getString("data")).getString("data");
        List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            String id = datum.getString("id");
            String name = datum.getString("name");
            InterServerRes interServer = new InterServerRes();
            interServer.setId(id);
            interServer.setName(name);
            interServerRes.add(interServer);
            List<InterServerRes> interServerRes1 = new ArrayList<>();
            String servers = datum.getString("small_partition");
            if(StringUtils.isNotEmpty(servers)) {
                List<String> list = JSONObject.parseArray(servers, String.class);
                for (String s : list) {
                    InterServerRes interServer1 = new InterServerRes();
                    interServer1.setId(s);
                    interServer1.setName(s);
                    interServerRes1.add(interServer1);
                    interServer.setInterServerRes(interServerRes1);
                }
            }
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        Map<String, Object> actions = new HashMap<>();

        actions.put("game_id",gameId);

        String appkey = salePartner.getSaleKey();
        String APPSECRET = salePartner.getOther();
        long l = System.currentTimeMillis();
        String timestamp = String.valueOf(l);
        params.put("game_id",gameId);
        params.put("kxhy-app-key",appkey);
        params.put("kxhy-nonce","02aca0b7");
        params.put("kxhy-timestamp",timestamp);

        String cover1 = MapUntils.getCover(params);
        cover1 = cover1.substring(0,cover1.length()-1);
        System.out.println(cover1);


        String cover = APPSECRET+cover1+APPSECRET;
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();


        String response = HttpRequest.post("https://kxxm.kxhyapp.com/openApi/gameDetail").header("kxhy-app-key", appkey).header("kxhy-nonce", "02aca0b7")
                .header("kxhy-timestamp", timestamp).header("kxhy-sign", sign).body(JSONObject.toJSONString(actions)).execute().body();
        System.out.println(response);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Integer status = JSONObject.parseObject(response).getInteger("code");
        if (0 == status) {
            String datas = JSONObject.parseObject(response).getString("data");
            String gameCate = JSONObject.parseObject(datas).getString("game_cate");
            Object o = JSONArray.parseArray(gameCate).get(0);
            String param_json = JSONObject.parseObject(o.toString()).getString("param_json");

            List<JSONObject> data = JSONObject.parseArray(param_json, JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){
                collect = data.stream().filter(d -> d.getString("type").equals("3")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                collect = data.stream().filter(d -> d.getString("type").equals("4")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("type").equals("1")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                String id = object.getString("id");
                String name = object.getString("name");
                String val = object.getString("val");

                if(val.contains(",")){
                    List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                    String[] split = val.split(",");
                    for(String s : split){
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setId(s);
                        interAttrRes1.setName(s);
                        interAttrResList1.add(interAttrRes1);
                    }
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
                interAttrRes.setId(id);
                interAttrRes.setName(name);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }

    public static String createSign(Map<String, Object> allParam, String appSecret) {
        StringBuilder sb = new StringBuilder();
        SortedMap<String, Object> params = new TreeMap<>(allParam);
        Set<Map.Entry<String, Object>> es = params.entrySet();
        for (Map.Entry<String, Object> entry : es) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("appSecret=").append(appSecret);
        return Objects.requireNonNull(DigestUtils.md5Hex(sb.toString())).toUpperCase();
    }
}